package com.chaoge.web.controller.system;

import com.chaoge.domain.system.Dept;
import com.chaoge.domain.system.Module;
import com.chaoge.domain.system.Role;
import com.chaoge.domain.system.User;
import com.chaoge.utils.BeanUtil;
import com.chaoge.web.controller.BaseServlet;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/user")
public class UserServlet extends BaseServlet {
    //private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        switch (operation){
            case "list":
                this.list(request,response);
                break;
            case "toAdd":
                this.toAdd(request,response);
                break;
            case "save":
                this.save(request,response);
                break;
            case "toEdit":
                this.toEdit(request,response);
                break;
            case "edit":
                this.edit(request,response);
                break;
            case "delete":
                this.delete(request,response);
                break;
            case "userRoleList":
                this.userRoleList(request,response);
                break;
            case "updateRole":
                this.updateRole(request,response);
                break;
            case "login":
                this.login(request,response);
                break;
            case "home":
                this.home(request,response);
                break;
            case "logout":
                this.logout(request,response);
                break;
        }
    }

    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().removeAttribute("loginUser");
        //跳转到List页面
        response.sendRedirect(request.getContextPath()+"/login.jsp");
    }

    private void home(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/home/home.jsp").forward(request, response);
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String email=request.getParameter("email");
        String password=request.getParameter("password");

        User user=userService.login(email,password);

        if (user!=null){
            request.getSession().setAttribute("loginUser",user);

            List<Module> moduleList=userService.findModuleById(user.getId());
            request.setAttribute("moduleList",moduleList);
            StringBuffer stringBuffer=new StringBuffer();
            for (Module module:moduleList){
                stringBuffer.append(module.getCurl());
                stringBuffer.append(',');
            }
            request.getSession().setAttribute("authorStr",stringBuffer.toString());
            //跳转到List页面
            request.getRequestDispatcher("/WEB-INF/pages/home/main.jsp").forward(request, response);

        }else {
            //跳转到List页面
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }

    private void updateRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userId = request.getParameter("userId");
        String[] roleIds = request.getParameterValues("roleIds");
        userService.updateRole(userId, roleIds);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
    }

    private void userRoleList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //获取数据
        String userId=request.getParameter("id");
        //查询单个数据
        User user = userService.findById(userId);
        //设置数据
        request.setAttribute("user",user);
        //获取角色列表
        List<Role> all = roleService.findAllRoleByUserId(userId);
        request.setAttribute("roleList",all);
        //跳转到List页面
        request.getRequestDispatcher("/WEB-INF/pages/system/user/role.jsp").forward(request,response);


    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取数据
        User user= BeanUtil.fillBean(request,User.class);
        //将数据传到userService层
        userService.delete(user);
        //跳转到List页面
        response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取数据
        User user= BeanUtil.fillBean(request,User.class,"yyyy-MM-dd");
        //将数据传到userService层
        userService.update(user);
        //跳转到List页面
        response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException  {
        List<Dept> all = deptService.findAll();
        request.setAttribute("deptList",all);
        //获取id
        String id = request.getParameter("id");
        //查询单个数据
        User user = userService.findById(id);
        //设置数据
        request.setAttribute("user",user);
        //页面跳转
        request.getRequestDispatcher("/WEB-INF/pages/system/user/update.jsp").forward(request,response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        //获取数据
        User user= BeanUtil.fillBean(request,User.class,"yyyy-MM-dd");
        //将数据传到userService层
        userService.save(user);
        //跳转到List页面
        response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        List<Dept> all = deptService.findAll();
        request.setAttribute("deptList",all);
        //页面跳转

        request.getRequestDispatcher("/WEB-INF/pages/system/user/add.jsp").forward(request,response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进入列表页
        //获取数据
        int page=1;
        int size=5;
        if (StringUtils.isNotBlank(request.getParameter("page"))){
            page=Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))){
            size=Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = userService.findAll(page, size);
        //将数据保存
        request.setAttribute("page",all);
        //页面跳转
        request.getRequestDispatcher("/WEB-INF/pages/system/user/list.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
